package com.yanxibao.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yanxibao.bean.User;
import com.yanxibao.common.Result;
import com.yanxibao.dto.LoginDTO;
import com.yanxibao.dto.UserDTO;
import com.yanxibao.dto.UserSearchDTO;
import com.yanxibao.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户;(_user)表控制层
 */
@Slf4j
@Api(tags = "用户对象功能接口")
@RestController
@CrossOrigin
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<UserDTO> login(@RequestBody LoginDTO loginDTO) {
        return userService.login(loginDTO);
    }

    /**
     * 通过ID查询单条数据
     * @return 实例对象
     */
    @ApiOperation("通过ID查询单条数据")
    @GetMapping("/queryById/{id}")
    public Result<User> selectUserById(@PathVariable String id) {
        return userService.selectUserById(id);
    }

    /**
     * 管理员分页查询筛选用户信息
     * @param currentSize
     * @param userSearchDTO
     * @return
     */
    @ApiOperation("管理员分页查询筛选用户信息")
    @GetMapping("/pageQuery/{currentSize}")
    public Result<IPage<User>> paginQuery(@PathVariable Integer currentSize,
                                          @RequestBody UserSearchDTO userSearchDTO) {
        return userService.paginQuery(currentSize, userSearchDTO);
    }

    /**
     * 更新数据
     * @param user 实例对象
     * @return 实例对象
     */
    @ApiOperation("更新用户信息")
    @PutMapping("/updateUser")
    public Result<User> edit(@RequestBody User user) {
        return userService.updateUser(user);
    }

    /**
     * 管理员删除用户信息
     * @param userId 主键
     * @return 是否成功
     */
    @ApiOperation("管理员删除用户信息")
    @DeleteMapping("/deleteUserById/{userId}")
    public Result<Boolean> deleteUserById(@PathVariable String userId) {
        return userService.deleteUserById(userId);
    }

}